import axios from 'axios';
import qs from 'query-string';
import {
  PageQueryResponseModel,
  BasePageQueryRequestModel,
} from '@/types/common';

export interface RouteModel {
  id: number;
  downstreamPathTemplate: string;
  upstreamPathTemplate: string;
  upstreamHttpMethod: string;
  downstreamHttpMethod: string;
  downstreamHttpVersion: string;
  requestIdKey: string;
  downstreamScheme: string;
  sort: number;
  enabled: boolean;
}

export interface PageQueryRouteParams extends BasePageQueryRequestModel {
  enabled: boolean | undefined;
  projectId: number | undefined;
  downstreamPathTemplate: string | undefined;
  upstreamPathTemplate: string | undefined;
  requestIdKey: string | undefined;
}

export function getPageRoute(params: PageQueryRouteParams) {
  return axios.get<PageQueryResponseModel<RouteModel>>(
    '/api/route/page-route',
    {
      params,
      paramsSerializer: (obj) => {
        return qs.stringify(obj);
      },
    }
  );
}

export interface RoutePropertieModel {
  id: number;
  key: string;
  value: string;
  routeId: number;
  type: number;
}

export interface RouteHostPortModel {
  id: number;
  host: string;
  port: number;
  routeId: number;
}

export interface RouteDetailModel {
  id: number;
  projectId: number;
  downstreamPathTemplate: string | undefined;
  upstreamPathTemplate: string | undefined;
  upstreamHttpMethod: string[] | undefined;
  downstreamHttpMethod: string | undefined;
  downstreamHttpVersion: string | undefined;
  requestIdKey: string | undefined;
  routeIsCaseSensitive: boolean | undefined;
  serviceName: string | undefined;
  serviceNamespace: string | undefined;
  downstreamScheme: string | undefined;
  cacheTtlseconds: number | undefined;
  cacheRegion: string | undefined;
  qosExceptionsallowedbeforebreaking: string | undefined;
  qosDurationofbreak: string | undefined;
  qosTimeoutvalue: string | undefined;
  loadbalancerType: string | undefined;
  loadbalancerKey: string | undefined;
  loadbalancerExpiry: number | undefined;
  ratelimitEnableratelimiting: boolean | undefined;
  ratelimitPeriod: string | undefined;
  ratelimitPeriodtimespan: string | undefined;
  ratelimitLimit: string | undefined;
  ratelimitWhitelist: string[];
  authenticationAuthenticationproviderkey: string | undefined;
  authenticationAllowedscopes: string[];
  httphandlerAllowautoredirect: boolean | undefined;
  httphandlerUsecookiecontainer: boolean | undefined;
  httphandlerUsetracing: boolean | undefined;
  httphandlerUseproxy: boolean | undefined;
  httphandlerMaxconnectionsperserver: number | undefined;
  upstreamHost: string | undefined;
  key: string | undefined;
  delegatingHandlers: string[];
  priority: number | undefined;
  timeout: number | undefined;
  dangerousAcceptAnyServerCertificateValidator: boolean | undefined;
  securityIpallowedlist: string[] | undefined;
  securityIpblockedlist: string[] | undefined;
  enabled: boolean;
  routeHostPorts: RouteHostPortModel[];
  routeProperties: RoutePropertieModel[];
  sort: number | undefined;
}

// 查询路由具体信息
export function getRoute(routeId: number) {
  return axios.get<RouteDetailModel>(`/api/route/route/${routeId}`);
}

export function insertRoute(data: RouteDetailModel) {
  return axios.post('/api/route/route', data);
}

export function updateRoute(data: RouteDetailModel) {
  return axios.put('/api/route/route', data);
}

// 启用或禁用路由
export function patchRoute(routeId: number, enabled: boolean) {
  return axios.patch(`/api/route/route/${routeId}/${enabled}`);
}

// 删除路由
export function deleteRoute(routeId: number) {
  return axios.delete(`/api/route/route/${routeId}`);
}

// 获取预览
export function getRoutePreview() {
  return axios.get<string>('/api/route/route-preview');
}
